/*
 *  The MIT License
 * 
 *  Copyright 2011 Edwin Bratini.
 * 
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 * 
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 * 
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
 */

/*
 * RegistroPlanes.java
 *
 * Created on 05/04/2011, 12:12:46 PM
 */
package com.prebea.syscafil.ui;

import com.prebea.syscafil.business.CategoriaPlanManager;
import com.prebea.syscafil.business.NumberFieldValidator;
import com.prebea.syscafil.business.DefaultComboFieldValueValidator;
import com.prebea.syscafil.business.EmptyFieldValidator;
import com.prebea.syscafil.business.FieldValidator;
import com.prebea.syscafil.business.FormFieldValidator;
import com.prebea.syscafil.business.PlanManager;
import com.prebea.syscafil.business.SubcategoriaPlanManager;
import com.prebea.syscafil.model.entities.CategoriaPlan;
import com.prebea.syscafil.model.entities.Plan;
import com.prebea.syscafil.model.entities.SubcategoriaPlan;
import java.awt.Color;
import java.awt.Toolkit;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.swing.JLabel;

/**
 *
 * @author Edwin Bratini
 */
public class RegistroPlanes extends javax.swing.JFrame {

    /** Creates new form RegistroPlanes */
    public RegistroPlanes() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        pnlInfoPlan = new javax.swing.JPanel();
        lblCategoriaPlan = new javax.swing.JLabel();
        cmbCategoriaPlan = new javax.swing.JComboBox();
        lblSubcategoria = new javax.swing.JLabel();
        cmbSubcategoriaPlan = new javax.swing.JComboBox();
        lblNombrePlan = new javax.swing.JLabel();
        txtNombrePlan = new javax.swing.JTextField();
        lblDescripcionPlan = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtaDescripcion = new javax.swing.JTextArea();
        lblPrecioPlan = new javax.swing.JLabel();
        ftfPrecioPlan = new javax.swing.JFormattedTextField();
        lblPrecioDepExtra = new javax.swing.JLabel();
        ftfPrecioDepExtra = new javax.swing.JFormattedTextField();
        btnNuevaCatPlan = new javax.swing.JButton();
        btnNuevaSubcatPlan = new javax.swing.JButton();
        lblNombreValMarker = new javax.swing.JLabel();
        lblDescripcionValMarker = new javax.swing.JLabel();
        lblCatPlanValMarker = new javax.swing.JLabel();
        lblSubcatPlanValMarker = new javax.swing.JLabel();
        lblPrecioValMarker = new javax.swing.JLabel();
        lblPrecioDepExtraValMarker = new javax.swing.JLabel();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        btnAceptar = new javax.swing.JButton();
        lblMensajeInsercion = new javax.swing.JLabel();
        btnSalir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Registro de Planes");
        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/resources/imagenes/prebea_logo.png")));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        pnlInfoPlan.setBorder(javax.swing.BorderFactory.createEtchedBorder(java.awt.Color.white, java.awt.Color.lightGray));

        lblCategoriaPlan.setText("Categoria Plan");

        cmbCategoriaPlan.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccion" }));
        cmbCategoriaPlan.setNextFocusableComponent(cmbSubcategoriaPlan);
        cmbCategoriaPlan.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbCategoriaPlanItemStateChanged(evt);
            }
        });
        cmbCategoriaPlan.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                cmbCategoriaPlanFocusGained(evt);
            }
        });

        lblSubcategoria.setText("Subcategoria");

        cmbSubcategoriaPlan.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccion" }));
        cmbSubcategoriaPlan.setNextFocusableComponent(ftfPrecioPlan);
        cmbSubcategoriaPlan.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                cmbSubcategoriaPlanFocusGained(evt);
            }
        });

        lblNombrePlan.setText("Nombre");

        lblDescripcionPlan.setText("Descripcion");

        jtaDescripcion.setColumns(20);
        jtaDescripcion.setRows(3);
        jScrollPane1.setViewportView(jtaDescripcion);

        lblPrecioPlan.setText("Precio");

        ftfPrecioPlan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ftfPrecioPlanActionPerformed(evt);
            }
        });

        lblPrecioDepExtra.setText("Precio Dep. Extra");

        btnNuevaCatPlan.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/imagenes/MBAdd.active.png"))); // NOI18N
        btnNuevaCatPlan.setBorderPainted(false);
        btnNuevaCatPlan.setNextFocusableComponent(btnNuevaSubcatPlan);
        btnNuevaCatPlan.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/imagenes/MBAdd.mousedown.png"))); // NOI18N
        btnNuevaCatPlan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevaCatPlanActionPerformed(evt);
            }
        });

        btnNuevaSubcatPlan.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/imagenes/MBAdd.active.png"))); // NOI18N
        btnNuevaSubcatPlan.setBorderPainted(false);
        btnNuevaSubcatPlan.setNextFocusableComponent(txtNombrePlan);
        btnNuevaSubcatPlan.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/imagenes/MBAdd.mousedown.png"))); // NOI18N
        btnNuevaSubcatPlan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevaSubcatPlanActionPerformed(evt);
            }
        });

        lblNombreValMarker.setForeground(new java.awt.Color(255, 51, 51));

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, txtNombrePlan, org.jdesktop.beansbinding.ObjectProperty.create(), lblNombreValMarker, org.jdesktop.beansbinding.BeanProperty.create("labelFor"));
        bindingGroup.addBinding(binding);

        lblDescripcionValMarker.setForeground(new java.awt.Color(255, 51, 51));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jtaDescripcion, org.jdesktop.beansbinding.ObjectProperty.create(), lblDescripcionValMarker, org.jdesktop.beansbinding.BeanProperty.create("labelFor"));
        bindingGroup.addBinding(binding);

        lblCatPlanValMarker.setForeground(new java.awt.Color(255, 51, 51));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, cmbCategoriaPlan, org.jdesktop.beansbinding.ObjectProperty.create(), lblCatPlanValMarker, org.jdesktop.beansbinding.BeanProperty.create("labelFor"));
        bindingGroup.addBinding(binding);

        lblSubcatPlanValMarker.setForeground(new java.awt.Color(255, 51, 51));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, cmbSubcategoriaPlan, org.jdesktop.beansbinding.ObjectProperty.create(), lblSubcatPlanValMarker, org.jdesktop.beansbinding.BeanProperty.create("labelFor"));
        bindingGroup.addBinding(binding);

        lblPrecioValMarker.setForeground(new java.awt.Color(255, 51, 51));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, ftfPrecioPlan, org.jdesktop.beansbinding.ObjectProperty.create(), lblPrecioValMarker, org.jdesktop.beansbinding.BeanProperty.create("labelFor"));
        bindingGroup.addBinding(binding);

        lblPrecioDepExtraValMarker.setForeground(new java.awt.Color(255, 51, 51));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, ftfPrecioDepExtra, org.jdesktop.beansbinding.ObjectProperty.create(), lblPrecioDepExtraValMarker, org.jdesktop.beansbinding.BeanProperty.create("labelFor"));
        bindingGroup.addBinding(binding);

        javax.swing.GroupLayout pnlInfoPlanLayout = new javax.swing.GroupLayout(pnlInfoPlan);
        pnlInfoPlan.setLayout(pnlInfoPlanLayout);
        pnlInfoPlanLayout.setHorizontalGroup(
            pnlInfoPlanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlInfoPlanLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlInfoPlanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblPrecioDepExtra, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblPrecioPlan, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblSubcategoria, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblCategoriaPlan, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblDescripcionPlan, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblNombrePlan, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlInfoPlanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
                    .addComponent(txtNombrePlan)
                    .addGroup(pnlInfoPlanLayout.createSequentialGroup()
                        .addGroup(pnlInfoPlanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(ftfPrecioDepExtra, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(ftfPrecioPlan, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cmbSubcategoriaPlan, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(cmbCategoriaPlan, javax.swing.GroupLayout.Alignment.LEADING, 0, 105, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(pnlInfoPlanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnNuevaSubcatPlan, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnNuevaCatPlan, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(18, 18, 18)
                .addGroup(pnlInfoPlanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblDescripcionValMarker)
                    .addComponent(lblNombreValMarker)
                    .addComponent(lblCatPlanValMarker)
                    .addComponent(lblSubcatPlanValMarker)
                    .addComponent(lblPrecioValMarker)
                    .addComponent(lblPrecioDepExtraValMarker))
                .addContainerGap(16, Short.MAX_VALUE))
        );
        pnlInfoPlanLayout.setVerticalGroup(
            pnlInfoPlanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlInfoPlanLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlInfoPlanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNombrePlan)
                    .addComponent(txtNombrePlan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblNombreValMarker))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlInfoPlanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlInfoPlanLayout.createSequentialGroup()
                        .addGroup(pnlInfoPlanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblDescripcionPlan)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnlInfoPlanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(pnlInfoPlanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(lblCategoriaPlan)
                                .addComponent(cmbCategoriaPlan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(lblCatPlanValMarker))
                            .addComponent(btnNuevaCatPlan, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(lblDescripcionValMarker))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlInfoPlanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlInfoPlanLayout.createSequentialGroup()
                        .addGroup(pnlInfoPlanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblSubcategoria)
                            .addComponent(cmbSubcategoriaPlan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblSubcatPlanValMarker))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnlInfoPlanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblPrecioPlan)
                            .addComponent(ftfPrecioPlan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblPrecioValMarker))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnlInfoPlanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblPrecioDepExtra)
                            .addComponent(ftfPrecioDepExtra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblPrecioDepExtraValMarker)))
                    .addComponent(btnNuevaSubcatPlan, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 361, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 341, Short.MAX_VALUE)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel))
                .addGap(3, 3, 3))
        );

        btnAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/imagenes/add_25x25.png"))); // NOI18N
        btnAceptar.setText("Aceptar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        lblMensajeInsercion.setForeground(new java.awt.Color(204, 204, 204));
        lblMensajeInsercion.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblMensajeInsercion.setText("*");

        btnSalir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/imagenes/salir2-32x32.png"))); // NOI18N
        btnSalir.setNextFocusableComponent(btnNuevaCatPlan);
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnAceptar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 202, Short.MAX_VALUE)
                .addComponent(btnSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(pnlInfoPlan, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblMensajeInsercion, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(145, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(pnlInfoPlan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblMensajeInsercion)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(statusPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ftfPrecioPlanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ftfPrecioPlanActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ftfPrecioPlanActionPerformed

    private void btnNuevaCatPlanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevaCatPlanActionPerformed
        // TODO add your handling code here:
        RegistroCategoriaPlan rcp = new RegistroCategoriaPlan(this, true);
        rcp.setLocationRelativeTo(this);
        rcp.setVisible(true);
        loadCategoriaPlanComboBox();
    }//GEN-LAST:event_btnNuevaCatPlanActionPerformed

    private void btnNuevaSubcatPlanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevaSubcatPlanActionPerformed
        // TODO add your handling code here:
        RegistroSubcategoriaPlan rsp = new RegistroSubcategoriaPlan(this, true);
        rsp.setLocationRelativeTo(this);
        rsp.setVisible(true);
        loadSubcategoriaPlanComboBox();
    }//GEN-LAST:event_btnNuevaSubcatPlanActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        loadCategoriaPlanComboBox();
        LimpiadorComponentes.limpiarComponentes(this);
        LimpiadorComponentes.limpiarValidationMarkers(this);
    }//GEN-LAST:event_formWindowOpened

    private void cmbCategoriaPlanFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_cmbCategoriaPlanFocusGained
        // TODO add your handling code here:
        btnNuevaCatPlan.setVisible(true);
    }//GEN-LAST:event_cmbCategoriaPlanFocusGained

    private void cmbSubcategoriaPlanFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_cmbSubcategoriaPlanFocusGained
        // TODO add your handling code here:
        btnNuevaSubcatPlan.setVisible(true);
    }//GEN-LAST:event_cmbSubcategoriaPlanFocusGained

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        // TODO add your handling code here:
        this.dispose();
}//GEN-LAST:event_btnSalirActionPerformed

    private void loadCategoriaPlanComboBox() {
        List<CategoriaPlan> catsPlan = new CategoriaPlanManager().getCategoriaPlanes();
        cmbCategoriaPlan.removeAllItems();
        cmbCategoriaPlan.addItem("Seleccion");
        Collections.sort(catsPlan);
        for (CategoriaPlan catPlan : catsPlan) {
            cmbCategoriaPlan.addItem(catPlan.getCapNombre());
        }
    }

    private void loadSubcategoriaPlanComboBox() {
        if (cmbCategoriaPlan.getSelectedItem() != null && new DefaultComboFieldValueValidator().validate(cmbCategoriaPlan.getSelectedItem().toString())) {
            List<SubcategoriaPlan> subCatsPlan = new SubcategoriaPlanManager().getSubcategoriaPlanByCategoriaPlan(new CategoriaPlanManager().getCategoriaPlanByNombre(cmbCategoriaPlan.getSelectedItem().toString()));
            cmbSubcategoriaPlan.removeAllItems();
            cmbSubcategoriaPlan.addItem("Seleccion");
            Collections.sort(subCatsPlan);
            for (SubcategoriaPlan subCatPlan : subCatsPlan) {
                cmbSubcategoriaPlan.addItem(subCatPlan.getSupNombre());
            }
        }
    }

    private boolean checkFormFields() {
        boolean validFields = true;

        FieldValidator emptynessVal, DefCombVal, decimalVal;
        emptynessVal = new EmptyFieldValidator();
        DefCombVal = new DefaultComboFieldValueValidator();
        FieldValidator[] emptynessArr = new FieldValidator[]{emptynessVal};
        decimalVal = new NumberFieldValidator("(\\d{1,})\\.?(\\d{1,})");

        HashMap<JLabel, FieldValidator[]> campos = new HashMap<JLabel, FieldValidator[]>();
        campos.put(lblNombreValMarker, emptynessArr);
        campos.put(lblDescripcionValMarker, emptynessArr);
        campos.put(lblCatPlanValMarker, new FieldValidator[]{DefCombVal});
        campos.put(lblSubcatPlanValMarker, new FieldValidator[]{DefCombVal});
        campos.put(lblPrecioValMarker, new FieldValidator[]{decimalVal});
        campos.put(lblPrecioDepExtraValMarker, new FieldValidator[]{decimalVal});

        validFields = FormFieldValidator.verifyFormFields(campos);

        return validFields;
    }

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        // TODO add your handling code here:
        if (!checkFormFields()) {
            lblMensajeInsercion.setText("Por favor corriga los campos marcados");
            lblMensajeInsercion.setForeground(Color.red);
            //lblMensajeInsercion.setVisible(true);
            new Thread(new LabelToolTipShower(lblMensajeInsercion, 3000)).start();
            return;
        } else {
            PlanManager pm = new PlanManager();

            if (pm.getPlanByNombre(txtNombrePlan.getText()) != null) {
                lblNombreValMarker.setText("*");
                lblNombreValMarker.setVisible(true);
                lblMensajeInsercion.setText("Ya existe un plan con nombre digitado");
                lblMensajeInsercion.setForeground(Color.red);
                lblMensajeInsercion.setVisible(true);
                return;
            }

            Plan plan = new Plan(txtNombrePlan.getText(), jtaDescripcion.getText().trim(), BigDecimal.valueOf(Double.parseDouble(ftfPrecioPlan.getText())),
                    BigDecimal.valueOf(Double.parseDouble(ftfPrecioDepExtra.getText())), 'A', "RegistroPlanes", new Date());
            
            CategoriaPlan catPlan = new CategoriaPlanManager().getCategoriaPlanByNombre(cmbCategoriaPlan.getSelectedItem().toString());
            plan.setCategoriaPlan(catPlan);
            catPlan.getPlanCollection().add(plan);

            SubcategoriaPlan subCatPlan = null;
            for (SubcategoriaPlan sp : catPlan.getSubcategoriaPlanCollection()) {
                if (sp.getSupNombre().equalsIgnoreCase(cmbSubcategoriaPlan.getSelectedItem().toString())) {
                    subCatPlan = sp;
                    break;
                }
            }
            // TODO: pendiente revisar, devuelve nulo y tengo que reiniciar la app por completo para que reconozca
            // quiza tenga que hacer merge o persist
            if (subCatPlan != null) {
                plan.setSubcategoriaPlan(subCatPlan);
                subCatPlan.getPlanCollection().add(plan);
            }
            
            pm.crearPlan(plan);

            lblMensajeInsercion.setText("Plan creado exitosamente");
            lblMensajeInsercion.setForeground(Color.GREEN);
            lblMensajeInsercion.setVisible(true);
            new Thread(new LabelToolTipShower(lblMensajeInsercion)).start();
            LimpiadorComponentes.limpiarComponentes(this);
            txtNombrePlan.requestFocusInWindow();
        }
    }//GEN-LAST:event_btnAceptarActionPerformed

    private void cmbCategoriaPlanItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbCategoriaPlanItemStateChanged
        // TODO add your handling code here:
        cmbSubcategoriaPlan.removeAllItems();
        cmbSubcategoriaPlan.addItem("Seleccion");
        loadSubcategoriaPlanComboBox();

    }//GEN-LAST:event_cmbCategoriaPlanItemStateChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new RegistroPlanes().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnNuevaCatPlan;
    private javax.swing.JButton btnNuevaSubcatPlan;
    private javax.swing.JButton btnSalir;
    private javax.swing.JComboBox cmbCategoriaPlan;
    private javax.swing.JComboBox cmbSubcategoriaPlan;
    private javax.swing.JFormattedTextField ftfPrecioDepExtra;
    private javax.swing.JFormattedTextField ftfPrecioPlan;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jtaDescripcion;
    private javax.swing.JLabel lblCatPlanValMarker;
    private javax.swing.JLabel lblCategoriaPlan;
    private javax.swing.JLabel lblDescripcionPlan;
    private javax.swing.JLabel lblDescripcionValMarker;
    private javax.swing.JLabel lblMensajeInsercion;
    private javax.swing.JLabel lblNombrePlan;
    private javax.swing.JLabel lblNombreValMarker;
    private javax.swing.JLabel lblPrecioDepExtra;
    private javax.swing.JLabel lblPrecioDepExtraValMarker;
    private javax.swing.JLabel lblPrecioPlan;
    private javax.swing.JLabel lblPrecioValMarker;
    private javax.swing.JLabel lblSubcatPlanValMarker;
    private javax.swing.JLabel lblSubcategoria;
    private javax.swing.JPanel pnlInfoPlan;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JTextField txtNombrePlan;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
